class_name CardPileView
extends Control

@export var card_pile: CardPile

@onready var title: Label = %Title
@onready var cards: GridContainer = %Cards
@onready var back_button: Button = %BackButton
@onready var card_tooltip_popup: CardTooltipPopup = %CardTooltipPopup

func _ready() -> void:
	back_button.pressed.connect(hide)
	
func _input(event: InputEvent) -> void:
	if event.is_action_pressed("ui_cancel"):
		if card_tooltip_popup.visible:
			card_tooltip_popup.hide_tooltip()
		else:
			hide()
	
func show_current_view(new_title: String, randomized: bool = false) -> void:
	for child in cards.get_children():
		child.queue_free()
	card_tooltip_popup.hide_tooltip()
	title.text = new_title
	_udpate_view.call_deferred(randomized)
	
func _udpate_view(randomized: bool) -> void:
	if not card_pile:
		return
	var all_cards := card_pile.cards.duplicate()
	if randomized:
		RNG.shuffle(all_cards)
	for card in all_cards:
		var new_card := AutoLoader.CARD_MENU_UI_SCENE.instantiate() as CardMenuUI
		cards.add_child(new_card)
		new_card.card = card
		new_card.tooltip_requested.connect(card_tooltip_popup.show_tooltip)
	show()


func _on_gui_input(event: InputEvent) -> void:
	if event.is_action_pressed("left_mouse"):
		hide()
